package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @ApiOperation("用户新增接口")
    @PostMapping
    public void addUser(@RequestBody UserFormDTO userFormDTO) {
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("用户删除接口")
    @DeleteMapping("/{id}")
    public void deleteUser(@ApiParam("用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("/{id}")
    public UserVO selectUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        User user = userService.getById(id);
        return BeanUtil.copyProperties(user, UserVO.class);
    }

    @ApiOperation("根据id查询用户接口和地址")
    @GetMapping("/address/{id}")
    public UserVO selectUserAndAdressById(@ApiParam("用户id") @PathVariable("id") Long id) {
        UserVO uservo = userService.getAddressById(id);
        return uservo;
    }

    @ApiOperation("根据ids批量查询用户接口")
    @GetMapping
    public List<UserVO> selectUserByIds(@ApiParam("用户ids") @RequestParam("ids") List<Long> ids) {
//        List<User> user = userService.listByIds(ids);
//        return BeanUtil.copyToList(user, UserVO.class);
        return userService.addressAndUserslistByIds(ids);
    }

    @ApiOperation("根据id扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void selectUserById(@ApiParam("用户id") @PathVariable("id") Long id,
                               @ApiParam("用户扣减金额money") @PathVariable("money") Integer money) {
        userService.deductBalance(id, money);
    }

    @ApiOperation("复杂条件批量查询用户接口")
    @GetMapping("/list")
    public List<UserVO> selectUser(UserQuery userQuery) {
        List<User> user = userService.queryUsers(userQuery.getName(), userQuery.getStatus(), userQuery.getMaxBalance(), userQuery.getMinBalance());
        return BeanUtil.copyToList(user, UserVO.class);
    }

    @ApiOperation("复杂条件_分页_查询用户接口")
    @GetMapping("/pagelist")
    public PageDTO<UserVO> selectUserPage(UserQuery userQuery) {
        return userService.queryUsersPage(userQuery);
    }

    @ApiOperation("批量新增用户接口")
    @GetMapping("/create")
    public void addBatchUser() {
        userService.addBatchUser();
    }

    // ... existing code ...

    @ApiOperation("分页批量查询用户接口")
    @GetMapping("/page")
    public IPage<User> selectUserPage(Page<User> page) {
//    return userService.addressAndUsersPage(page, ids);
        userService.page(page);
        return page;
    }

// ... existing code ...


}
